/*
 * Copyright 2014 alexpad@gmail.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package alexpad.karaokarc.gui.songList;

import alexpad.karaokarc.midi.MidiSongInfo;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.Document;

/**
 *
 * @author alexpad@gmail.com
 */
public class SearchPanel extends javax.swing.JPanel {
	
	private SongListPanel songListPanel;
	
	private final DocumentListener searchDocumentListener = new DocumentListener() {

		@Override
		public void insertUpdate(DocumentEvent e) {
			updateRowFilter();
		}

		@Override
		public void removeUpdate(DocumentEvent e) {
			updateRowFilter();
		}

		@Override
		public void changedUpdate(DocumentEvent e) {
			updateRowFilter();
		}
	};
	
	protected void updateRowFilter(){
		MidiSongInfo.Type type = null;
		if(typeCombo.getSelectedIndex()>0){
			type = (MidiSongInfo.Type) typeCombo.getSelectedItem();
		}
		songListPanel.updateRowFilter(
			new SearchRowFilter(artistText.getText(), titleText.getText(), type)
		);
	}

	/**
	 * Creates new form SearchPanel
	 */
	protected SearchPanel() {
		initComponents();
		typeCombo.setModel(new DefaultComboBoxModel(new Object[]{"Tutti", MidiSongInfo.Type.KAR, MidiSongInfo.Type.MID}));
		
		Document document = artistText.getDocument();
		document.addDocumentListener(searchDocumentListener);
		document = titleText.getDocument();
		document.addDocumentListener(searchDocumentListener);
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        artistText = new javax.swing.JTextField();
        titleText = new javax.swing.JTextField();
        artistLabel = new javax.swing.JLabel();
        titleLabel = new javax.swing.JLabel();
        clearArtistBtn = new javax.swing.JButton();
        clearTitleBtn = new javax.swing.JButton();
        typeLabel = new javax.swing.JLabel();
        typeCombo = new javax.swing.JComboBox();

        artistText.setToolTipText("");
        artistText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                artistTextActionPerformed(evt);
            }
        });

        titleText.setToolTipText("");

        artistLabel.setText("Artista");

        titleLabel.setText("Titolo");

        clearArtistBtn.setText("C");
        clearArtistBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearArtistBtnActionPerformed(evt);
            }
        });

        clearTitleBtn.setText("C");
        clearTitleBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearTitleBtnActionPerformed(evt);
            }
        });

        typeLabel.setText("Tipo");

        typeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        typeCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                typeComboActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(artistLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(artistText, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                        .addGap(2, 2, 2)
                        .addComponent(clearArtistBtn)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(titleText, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                        .addGap(2, 2, 2)
                        .addComponent(clearTitleBtn))
                    .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(typeCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(typeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(artistLabel)
                    .addComponent(titleLabel)
                    .addComponent(typeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(artistText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(titleText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(clearArtistBtn)
                    .addComponent(clearTitleBtn)
                    .addComponent(typeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void artistTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_artistTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_artistTextActionPerformed

    private void clearArtistBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearArtistBtnActionPerformed
        artistText.setText(null);
    }//GEN-LAST:event_clearArtistBtnActionPerformed

    private void clearTitleBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearTitleBtnActionPerformed
        titleText.setText(null);
    }//GEN-LAST:event_clearTitleBtnActionPerformed

    private void typeComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_typeComboActionPerformed
		updateRowFilter();
    }//GEN-LAST:event_typeComboActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel artistLabel;
    private javax.swing.JTextField artistText;
    private javax.swing.JButton clearArtistBtn;
    private javax.swing.JButton clearTitleBtn;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JTextField titleText;
    private javax.swing.JComboBox typeCombo;
    private javax.swing.JLabel typeLabel;
    // End of variables declaration//GEN-END:variables

	
	public JTextField getArtistText() {
		return artistText;
	}

	public JTextField getTitleText() {
		return titleText;
	}
	
	public void setSongListPanel(SongListPanel songListPanel){
		this.songListPanel = songListPanel;
	}
}


